package lab7p1;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.filechooser.FileNameExtensionFilter;

public class RectangleGUI extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static void main(String[] args) {
		new RectangleGUI();
	}

	/*
	 * More data members
	 */
	JMenuBar  menuBar;
	JMenu     fileMenu;
	JMenuItem newMenuItem;
	JMenuItem openMenuItem;
	JMenuItem saveMenuItem;
	
	public RectangleGUI(){
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setBounds( 200, 100, 400, 300 );
		
		
		menuBar = new JMenuBar();
		fileMenu = new JMenu( "File" );
		newMenuItem = new JMenuItem("New");
		newMenuItem.addActionListener( this );
		openMenuItem = new JMenuItem("Open ...");
		openMenuItem.addActionListener( this );
		saveMenuItem = new JMenuItem("Save As");
		saveMenuItem.addActionListener( this );
		fileMenu.add( newMenuItem );
		fileMenu.add( openMenuItem );
		fileMenu.add( saveMenuItem );
			
		menuBar.add( fileMenu );
		setJMenuBar( menuBar );

		FileNameExtensionFilter filter = new FileNameExtensionFilter( "Rectangle objects", "rct");
		filechooser.setFileFilter( filter );
		setVisible( true );
	}

	private Rectangle2D[] rectangles;
	private final int ARRAY_SIZE = 7;
	private JFileChooser filechooser = new JFileChooser();

	@Override
	public void actionPerformed(ActionEvent e) {
		JMenuItem source = (JMenuItem) e.getSource();
		if (source == newMenuItem){
			rectangles = null;
			repaint();
		} else if (source == openMenuItem) {
			int openResult = filechooser.showOpenDialog( this );
			if (openResult == JFileChooser.APPROVE_OPTION ){
				File openFile = filechooser.getSelectedFile();
				try {
					ObjectInputStream ois = new ObjectInputStream( new FileInputStream( openFile ) );
					rectangles = (Rectangle2D[]) ois.readObject();
					ois.close();
				} catch (FileNotFoundException fe){
					fe.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					e1.printStackTrace();
				}				
			}
			repaint();
		} else if (source == saveMenuItem) {
			int saveResult = filechooser.showSaveDialog( this );
			if (saveResult == JFileChooser.APPROVE_OPTION ){
				File saveFile = filechooser.getSelectedFile();
				try {
					ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream( saveFile ) );
					oos.writeObject( rectangles );
					oos.close();
				} catch (FileNotFoundException fe){
					fe.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			repaint();
		}		
	}
	
	public void paint(Graphics g){
		super.paint(g);
		Graphics2D g2d = (Graphics2D) g;
		
		if (rectangles == null){
			// create rectangles - random locations and dimensions
			rectangles = new Rectangle2D[ARRAY_SIZE];
			for (int i=0; i<rectangles.length; i++){
				final double x = Math.random() * getWidth();
				final double y = Math.random() * getHeight();
				final double w = Math.random() * (getWidth() - x);
				final double h = Math.random() * (getHeight() - y);
				rectangles[i] = new Rectangle2D.Double( x, y, w, h );
			}
		}
		
		// Draw the rectangles we have
		if (rectangles != null)
			for (int i=0; i<rectangles.length; i++)
				g2d.draw( rectangles[i] );
	}
}
